import time
# 导⼊selenium包
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
# 打开指定（Firefox）浏览器
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver = webdriver.Chrome()
driver.maximize_window()
# 指定加载⻚⾯
# driver.get("https://www.csdn.net")
driver.get("https://www.baidu.com")
#定位方式：
# 1.通过id属性获取搜索输⼊框
# input_text = driver.find_element(By.ID, "toolbar-search-input")
# 2.通过name属性获取
# input_text = driver.find_element(By.NAME, "wd")
# 3.通过class定位
# input_text = driver.find_element(By.CLASS_NAME, "s_ipt")
# 4.通过标签 tag 定位
# driver.find_element(By.TAG_NAME,"button").click()
# 5.1通过link精确定位
# driver.find_element(By.LINK_TEXT,"新闻").click()
# 5.2通过link模糊定位
# driver.find_element(By.PARTIAL_LINK_TEXT,"闻").click()
# 6.通过css定位:
#6.1 css通过id定位，前面加#号,通过claa定位，前面加.号,
# input_text = driver.find_element(By.CSS_SELECTOR,"#kw")
# input_text.send_keys("test")
# input_text = driver.find_element(By.CSS_SELECTOR,".s_ipt")
# input_text.send_keys("test")
#6.2 css通过标签加属性定位
# search_box = driver.find_element(By.CSS_SELECTOR, "input#kw") # 百度搜索框的CSS 选择器
# search_box.send_keys("Python ⾃动化测试") # 输⼊搜索内容
# search_box.send_keys(Keys.RETURN) # 模拟按下回⻋键

# first_result = driver.find_element(By.CSS_SELECTOR, "input[value='百度一下']") # 第⼀个搜索结果的 CSS 选择器
# first_result.click()

#6.3 css层级定位
#通过⽗标签[⽗标签属性名=⽗标签属性值]>（或者空格）⼦标签 我们以百度搜索按钮为例：
# driver.find_element(By.CSS_SELECTOR,"span[class='bg s_btn_wr']>input")
#通过搜索按钮的⽗标签中的class属性定位，然后找到其⼦标签input，也就是我们的搜索按钮元素所在。

# 7.xpath定位
# search_box = driver.find_element(By.CSS_SELECTOR, "input#kw") # 百度搜索框的CSS 选择器
# search_box.send_keys("Python ⾃动化测试") # 输⼊搜索内容
# search_box.send_keys(Keys.RETURN) # 模拟按下回⻋键
# # 等待搜索结果加载
# time.sleep(3) # 同样建议使⽤ WebDriverWait
# #绝对路径
# search_box = driver.find_element(By.XPATH,'/html/body/div[3]/div[3]/div[1]/div[3]/div[3]/div/h3/a/div/div/p')
# #相对路径
# search_box = driver.find_element(By.XPATH,'//*[@id="2"]/div/h3/a/div/div/p/span/span')
# search_box.click()
# 8.xpath模糊匹配
# #通过id包含” search ”来定位：
# driver.find_element(By.XPATH,"//input[contains(@id,'search')]")
# #通过class包含”operate_zn”来定位：
# driver.find_element(By.XPATH,"//input[contains(@class,'operate_zn')]")


# 8.等待

# #8.1 强制等待
# time.sleep(5)
# #8.2 隐式等待
# driver.implicitly_wait(6)
# #8.3 显示等待
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# WebDriverWait(driver,10,5).until(EC.presence_of_element_located((By.XPATH,'//*[@id="su"]')))

# 关闭浏览器
time.sleep(7)
driver.quit()